package com.fanling.venus.web.controller.project;

import com.fanling.venus.common.base.AjaxResult;
import com.fanling.venus.frame.web.BaseController;
import com.fanling.venus.frame.web.TableDataInfo;
import com.fanling.venus.project.domain.FinancialBalance;
import com.fanling.venus.project.service.FinancialBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/project/balance")
public class FinancialBalanceController extends BaseController {

    private String prefix = "project/balance";

    @Autowired
    private FinancialBalanceService financialBalanceService;

    @GetMapping()
    public String project() {
        return prefix + "/balance";
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/balance-add";
    }

    @GetMapping("/edit/{balanceId}")
    public String edit(@PathVariable("balanceId") Long balanceId, ModelMap mmap) {
        mmap.put("balance", financialBalanceService.selectFinancialBalanceById(balanceId));
        return prefix + "/balance-edit";
    }

    @GetMapping("/charts")
    public String charts() {
        return prefix + "/balance-charts";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinancialBalance financialBalance) {
        startPage();
        List<FinancialBalance> list = financialBalanceService.selectFinancialBalanceList(financialBalance);
        return getDataTable(list);
    }

    /**
     * 新增财务
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FinancialBalance financialBalance) {
        if ("01".equals(financialBalance.getType())) {
            financialBalance.setMoney(0 - financialBalance.getMoney());
        }
        return toAjax(financialBalanceService.insertFinancialBalance(financialBalance));
    }

    /**
     * 修改财务
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FinancialBalance financialBalance) {
        if ("01".equals(financialBalance.getType())) {
            financialBalance.setMoney(0 - financialBalance.getMoney());
        }
        return toAjax(financialBalanceService.updateFinancialBalance(financialBalance));
    }

    /**
     * 删除财务
     *
     * @param balanceId
     * @return
     */
    @PostMapping("/remove/{balanceId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("balanceId") Long balanceId) {
        return toAjax(financialBalanceService.deleteFinancialBalanceById(balanceId));
    }
    //////////////图表echarts////////////////////////////////////////

    /**
     * 获取类别统计数据
     *
     * @return
     */
    @PostMapping("/charts/typedata")
    @ResponseBody
    public Map<String, String> getTypeData() {
        //所有类别
        Map<String, String> typeNames = new HashMap<>();
        typeNames.put("01", "餐饮");
        typeNames.put("02", "购物");
        typeNames.put("03", "交通");
        typeNames.put("04", "娱乐");
        typeNames.put("05", "通讯");
        Map<String, String> typeData = new HashMap<>();
        typeNames.forEach((key, value) -> {
            typeData.put(value, financialBalanceService.selectFinancialBalanceSumByType(key));
        });
        return typeData;
    }

    /**
     * 按日统计
     *
     * @return
     */
    @PostMapping("/charts/daysdata")
    @ResponseBody
    public AjaxResult getDaysData() {
        List<FinancialBalance> daysDataList = financialBalanceService.selectFinancialBalanceSumByDays();
        return new AjaxResult().put("data", daysDataList);
    }
}
